import { Action, getModule, Module, Mutation, VuexModule } from 'vuex-module-decorators';
import store from '@/store';
import { GET_CACHE, getUser, removeUser, setUser, verifyMenuAuth, verifyUser, getSystem, setSystem } from '@/utils/user-util';
import axios from '@/utils/request';
import { TagsViewModule } from '@/store/modules/tags-view';
import { SGU_CACHE, DELETE_CACHE } from '@/utils/diction-const-util';

export interface IUserInfo {
    sessionId?: string;
    superuser?: boolean;
    themeType?: string;
    userCode?: string;
    userName?: string;
    resources?: any;

    [params: string]: any;
}

export interface IUser {
    user: IUserInfo;
    loginState: false;
}

@Module({ dynamic: true, name: 'user', namespaced: true, store })
class UserStore extends VuexModule {
    public user: IUserInfo = getUser();
    public loginState: boolean = !!GET_CACHE('user');
    public system = getSystem();

    /**
     * 设置用户信息
     * @param userInfo
     */
    @Mutation
    updateUserInfo(user: IUserInfo): void {
        if (user) {
            this.user = user;
            this.loginState = true;
        } else {
            this.user = {};
            this.loginState = false;
        }
        setUser(user);
        // 缓存用户产品数据
        SGU_CACHE('FundInfoByUser', '/fundInfo/list/byUser/like', [], 'autoRefresh');
    }

    /**
     * 退出登录
     */
    @Mutation
    deleteUserInfo(): void {
        this.user = {};
        this.loginState = false;
        // 删除用户产品数据
        DELETE_CACHE('FundInfoByUser');
        TagsViewModule.logout();
        removeUser();
    }

    /**
     * 验证session和菜单是否存在
     */
    @Action({ rawError: true })
    async validateSession(): Promise<any> {
        // 验证 session
        try {
            const validateUser = await verifyUser();

            this.context.commit('updateUserInfo', validateUser);
        } catch (e) {
            return Promise.reject(e);
        }

        try {
            const validateMenuAuth = await verifyMenuAuth();
            return Promise.resolve(validateMenuAuth);
        } catch (e) {
            return Promise.reject(e);
        }
    }

    /**
     * 修改主题
     * @param type 需要切换的主题
     */
    @Action({ rawError: true })
    async updateThemeType(type: string) {
        const themeType = type === 'light' ? '1' : '0';
        axios.get('/outFrame/update/theme/' + themeType).then(result => {
            const user = this.userInfo;
            user.themeType = themeType;
            this.context.commit('updateUserInfo', user);
            document.body.setAttribute('data-theme', type);

            // 改变子页面的主题
            try {
                const frames = window.frames;
                for (let i = 0; i < frames.length; i++) {
                    frames[i].document.body.setAttribute('data-theme', type);
                }
            } catch (e) {}
        });
    }

    /**
     * 获取用户信息
     */
    get userInfo(): IUserInfo {
        return this.user;
    }

    /**
     * 获取用户登录状态
     */
    get userLoginState(): boolean {
        return this.loginState;
    }

    /**
     * 获取主题
     */
    get getThemeType(): string {
        if (this.user.themeType === '1') {
            return 'light';
        } else {
            return 'blue';
        }
    }

    /**
     * 更新系统参数
     */
    @Mutation
    updateSystemParam(list): void {
        if (list) {
            this.system = list;
        } else {
            this.system = [];
        }
        setSystem(list);
        // 缓存系统参数
        // SGU_CACHE('system', '/systemParam/list', []);
    }

    /**
     * 获取系统参数
     */
    get systemParam() {
        return this.system;
    }
}

export const UserModule = getModule(UserStore);
